home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / apport_python_hook.py next >
Encoding:
Python Source  |  2009-04-06  |  12.5 KB  |  340 lines

  1. '''Python sys.excepthook hook to generate apport crash dumps.
  2.  
  3. See https://wiki.ubuntu.com/AutomatedProblemReports for details.
  4.  
  5. Copyright (c) 2006 Canonical Ltd.
  6. Authors: Robert Collins <robert@ubuntu.com>
  7.          Martin Pitt <martin.pitt@ubuntu.com>
  8.  
  9. This program is free software; you can redistribute it and/or modify it
  10. under the terms of the GNU General Public License as published by the
  11. Free Software Foundation; either version 2 of the License, or (at your
  12. option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
  13. the full text of the license.
  14. '''
  15.  
  16. import os
  17. import sys
  18.  
  19. def apport_excepthook(exc_type, exc_obj, exc_tb):
  20.     '''Catch an uncaught exception and make a traceback.'''
  21.  
  22.     # create and save a problem report. Note that exceptions in this code
  23.     # are bad, and we probably need a per-thread reentrancy guard to
  24.     # prevent that happening. However, on Ubuntu there should never be
  25.     # a reason for an exception here, other than [say] a read only var
  26.     # or some such. So what we do is use a try - finally to ensure that
  27.     # the original excepthook is invoked, and until we get bug reports
  28.     # ignore the other issues.
  29.  
  30.     # import locally here so that there is no routine overhead on python
  31.     # startup time - only when a traceback occurs will this trigger.
  32.     try:
  33.         # ignore 'safe' exit types.
  34.         if exc_type in (KeyboardInterrupt, ):
  35.             return
  36.  
  37.         # do not do anything if apport was disabled
  38.         from apport.packaging_impl import impl as packaging
  39.         if not packaging.enabled():
  40.             return
  41.  
  42.         from cStringIO import StringIO
  43.         import re, tempfile, traceback
  44.         from apport.fileutils import likely_packaged
  45.  
  46.         # apport will look up the package from the executable path.
  47.         try:
  48.             binary = os.path.realpath(os.path.join(os.getcwdu(), sys.argv[0]))
  49.         except (TypeError, AttributeError, IndexError):
  50.             # the module has mutated sys.argv, plan B
  51.             try:
  52.                 binary = os.readlink('/proc/%i/exe' % os.getpid())
  53.             except OSError:
  54.                 return
  55.  
  56.         # for interactive python sessions, sys.argv[0] == ''; catch that and
  57.         # other irregularities
  58.         if not os.access(binary, os.X_OK) or not os.path.isfile(binary):
  59.             return
  60.  
  61.         # filter out binaries in user accessible paths
  62.         if not likely_packaged(binary):
  63.             return
  64.  
  65.         import apport.report
  66.  
  67.         pr = apport.report.Report()
  68.         # append a basic traceback. In future we may want to include
  69.         # additional data such as the local variables, loaded modules etc.
  70.         tb_file = StringIO()
  71.         traceback.print_exception(exc_type, exc_obj, exc_tb, file=tb_file)
  72.         pr['Traceback'] = tb_file.getvalue().strip()
  73.         pr.add_proc_info()
  74.         pr.add_user_info()
  75.         # override the ExecutablePath with the script that was actually running.
  76.         pr['ExecutablePath'] = binary
  77.         pr['PythonArgs'] = '%r' % sys.argv
  78.         if pr.check_ignored():
  79.             return
  80.         mangled_program = re.sub('/', '_', binary)
  81.         # get the uid for now, user name later
  82.         user = os.getuid()
  83.         pr_filename = '/var/crash/%s.%i.crash' % (mangled_program, user)
  84.         if os.path.exists(pr_filename):
  85.             if apport.fileutils.seen_report(pr_filename):
  86.                 # remove the old file, so that we can create the new one with
  87.                 # os.O_CREAT|os.O_EXCL
  88.                 os.unlink(pr_filename)
  89.             else:
  90.                 # don't clobber existing report
  91.                 return
  92.         report_file = os.fdopen(os.open(pr_filename,
  93.             os.O_WRONLY|os.O_CREAT|os.O_EXCL), 'w')
  94.         os.chmod(pr_filename, 0600)
  95.         try:
  96.             pr.write(report_file)
  97.         finally:
  98.             report_file.close()
  99.  
  100.     finally:
  101.         # resume original processing to get the default behaviour,
  102.         # but do not trigger an AttributeError on interpreter shutdown.
  103.         if sys:
  104.             sys.__excepthook__(exc_type, exc_obj, exc_tb)
  105.  
  106.  
  107. def install():
  108.     '''Install the python apport hook.'''
  109.  
  110.     sys.excepthook = apport_excepthook
  111.  
  112. #
  113. # Unit test
  114. #
  115.  
  116. if __name__ == '__main__':
  117.     import unittest, tempfile, subprocess, os.path, stat
  118.     import apport.fileutils, problem_report
  119.  
  120.     class _PythonHookTest(unittest.TestCase):
  121.         def test_env(self):
  122.             '''Check the test environment.'''
  123.  
  124.             self.assertEqual(apport.fileutils.get_all_reports(), [],
  125.                 'No crash reports already present')
  126.  
  127.         def _test_crash(self, extracode='', scriptname=None):
  128.             '''Create a test crash.'''
  129.  
  130.             # put the script into /var/crash, since that isn't ignored in the
  131.             # hook
  132.             if scriptname:
  133.                 script = scriptname
  134.                 fd = os.open(scriptname, os.O_CREAT|os.O_WRONLY)
  135.             else:
  136.                 (fd, script) = tempfile.mkstemp(dir=apport.fileutils.report_dir)
  137.             try:
  138.                 os.write(fd, '''#!/usr/bin/python
  139. def func(x):
  140.     raise Exception, 'This should happen.'
  141.  
  142. %s
  143. func(42)
  144. ''' % extracode)
  145.                 os.close(fd)
  146.                 os.chmod(script, 0755)
  147.  
  148.                 p = subprocess.Popen([script, 'testarg1', 'testarg2'],
  149.                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  150.                 err = p.communicate()[1]
  151.                 self.assertEqual(p.returncode, 1,
  152.                     'crashing test python program exits with failure code')
  153.                 self.assert_('Exception: This should happen.' in err)
  154.                 self.failIf('OSError' in err, err)
  155.             finally:
  156.                 os.unlink(script)
  157.  
  158.             return script
  159.  
  160.         def test_general(self):
  161.             '''general operation of the Python crash hook.'''
  162.  
  163.             script = self._test_crash()
  164.  
  165.             # did we get a report?
  166.             reports = apport.fileutils.get_new_reports()
  167.             pr = None
  168.             try:
  169.                 self.assertEqual(len(reports), 1, 'crashed Python program produced a report')
  170.                 self.assertEqual(stat.S_IMODE(os.stat(reports[0]).st_mode),
  171.                     0600, 'report has correct permissions')
  172.  
  173.                 pr = problem_report.ProblemReport()
  174.                 pr.load(open(reports[0]))
  175.             finally:
  176.                 for r in reports:
  177.                     os.unlink(r)
  178.  
  179.             # check report contents
  180.             expected_keys = ['InterpreterPath', 'PythonArgs',
  181.                 'Traceback', 'ProblemType', 'ProcEnviron', 'ProcStatus',
  182.                 'ProcCmdline', 'Date', 'ExecutablePath', 'ProcMaps',
  183.                 'UserGroups']
  184.             self.assert_(set(expected_keys).issubset(set(pr.keys())),
  185.                 'report has necessary fields')
  186.             self.assert_('bin/python' in pr['InterpreterPath'])
  187.             self.assertEqual(pr['ExecutablePath'], script)
  188.             self.assertEqual(pr['PythonArgs'], "['%s', 'testarg1', 'testarg2']" % script)
  189.             self.assert_(pr['Traceback'].startswith('Traceback'))
  190.             self.assert_("func\n    raise Exception, 'This should happen." in pr['Traceback'])
  191.  
  192.         def test_existing(self):
  193.             '''Python crash hook overwrites seen existing files.'''
  194.  
  195.             script = self._test_crash()
  196.  
  197.             # did we get a report?
  198.             to_del = set()
  199.             try:
  200.                 reports = apport.fileutils.get_new_reports()
  201.                 to_del.update(reports)
  202.                 self.assertEqual(len(reports), 1, 'crashed Python program produced a report')
  203.                 self.assertEqual(stat.S_IMODE(os.stat(reports[0]).st_mode),
  204.                     0600, 'report has correct permissions')
  205.  
  206.                 # touch report -> "seen" case
  207.                 apport.fileutils.mark_report_seen(reports[0])
  208.  
  209.                 reports = apport.fileutils.get_new_reports()
  210.                 to_del.update(reports)
  211.                 self.assertEqual(len(reports), 0)
  212.  
  213.                 script = self._test_crash(scriptname=script)
  214.                 reports = apport.fileutils.get_new_reports()
  215.                 to_del.update(reports)
  216.                 self.assertEqual(len(reports), 1)
  217.  
  218.                 # "unseen" case
  219.                 script = self._test_crash(scriptname=script)
  220.                 reports = apport.fileutils.get_new_reports()
  221.                 self.assertEqual(len(reports), 1)
  222.                 to_del.update(reports)
  223.             finally:
  224.                 for r in to_del:
  225.                     os.unlink(r)
  226.  
  227.         def test_no_argv(self):
  228.             '''with zapped sys.argv.'''
  229.  
  230.             self._test_crash('import sys\nsys.argv = None')
  231.  
  232.             # did we get a report?
  233.             reports = apport.fileutils.get_new_reports()
  234.             pr = None
  235.             try:
  236.                 self.assertEqual(len(reports), 1, 'crashed Python program produced a report')
  237.                 self.assertEqual(stat.S_IMODE(os.stat(reports[0]).st_mode),
  238.                     0600, 'report has correct permissions')
  239.  
  240.                 pr = problem_report.ProblemReport()
  241.                 pr.load(open(reports[0]))
  242.             finally:
  243.                 for r in reports:
  244.                     os.unlink(r)
  245.  
  246.             # check report contents
  247.             expected_keys = ['InterpreterPath',
  248.                 'Traceback', 'ProblemType', 'ProcEnviron', 'ProcStatus',
  249.                 'ProcCmdline', 'Date', 'ExecutablePath', 'ProcMaps',
  250.                 'UserGroups']
  251.             self.assert_(set(expected_keys).issubset(set(pr.keys())),
  252.                 'report has necessary fields')
  253.             self.assert_('bin/python' in pr['InterpreterPath'])
  254.             self.assert_(pr['Traceback'].startswith('Traceback'))
  255.  
  256.         def _assert_no_reports(self):
  257.             '''Assert that there are no crash reports.'''
  258.  
  259.             reports = apport.fileutils.get_new_reports()
  260.             try:
  261.                 self.assertEqual(len(reports), 0,
  262.                     'no crash reports present (cwd: %s)' % os.getcwd())
  263.             finally:
  264.                 # clean up in case we fail
  265.                 for r in reports:
  266.                     pass
  267.                     #os.unlink(r)
  268.  
  269.         def test_interactive(self):
  270.             '''interactive Python sessions never generate a report.'''
  271.  
  272.             orig_cwd = os.getcwd()
  273.             try:
  274.                 for d in ('/tmp', '/usr/local', '/usr'):
  275.                     os.chdir(d)
  276.                     p = subprocess.Popen(['python'], stdin=subprocess.PIPE,
  277.                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  278.                     (out, err) = p.communicate('raise ValueError')
  279.                     assert p.returncode != 0
  280.                     assert out == ''
  281.                     assert 'ValueError' in err
  282.                     self._assert_no_reports()
  283.             finally:
  284.                 os.chdir(orig_cwd)
  285.  
  286.         def test_ignoring(self):
  287.             '''the Python crash hook respects the ignore list.'''
  288.  
  289.             # put the script into /var/crash, since that isn't ignored in the
  290.             # hook
  291.             (fd, script) = tempfile.mkstemp(dir=apport.fileutils.report_dir)
  292.             ifpath = os.path.expanduser(apport.report._ignore_file)
  293.             orig_ignore_file = None
  294.             try:
  295.                 os.write(fd, '''#!/usr/bin/python
  296. def func(x):
  297.     raise Exception, 'This should happen.'
  298.  
  299. func(42)
  300. ''')
  301.                 os.close(fd)
  302.                 os.chmod(script, 0755)
  303.  
  304.                 # move aside current ignore file
  305.                 if os.path.exists(ifpath):
  306.                     orig_ignore_file = ifpath + '.apporttest'
  307.                     os.rename(ifpath, orig_ignore_file)
  308.  
  309.                 # ignore
  310.                 r = apport.report.Report()
  311.                 r['ExecutablePath'] = script
  312.                 r.mark_ignore()
  313.                 r = None
  314.  
  315.                 p = subprocess.Popen([script, 'testarg1', 'testarg2'],
  316.                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  317.                 err = p.communicate()[1]
  318.                 self.assertEqual(p.returncode, 1,
  319.                     'crashing test python program exits with failure code')
  320.                 self.assert_('Exception: This should happen.' in err)
  321.  
  322.             finally:
  323.                 os.unlink(script)
  324.                 # clean up our ignore file
  325.                 if os.path.exists(ifpath):
  326.                     os.unlink(ifpath)
  327.                 if orig_ignore_file:
  328.                     os.rename(orig_ignore_file, ifpath)
  329.  
  330.             # did we get a report?
  331.             reports = apport.fileutils.get_new_reports()
  332.             pr = None
  333.             try:
  334.                 self.assertEqual(len(reports), 0)
  335.             finally:
  336.                 for r in reports:
  337.                     os.unlink(r)
  338.  
  339.     unittest.main()
  340.